﻿using System;
using System.IO;
using LitJson;
using System.Collections;

namespace CzfSDK
{
	public class Config
	{
		private JsonData data;
		public string jsonStr;

		public Config (string sPath)
		{
			using (StreamReader sr = new StreamReader (File.OpenRead (sPath))) {
				jsonStr = sr.ReadToEnd ();
				sr.Close ();
				data = JsonMapper.ToObject (jsonStr);
			}
		}

		public Config (StreamReader sr)
		{
			jsonStr = sr.ReadToEnd ();
			data = JsonMapper.ToObject (jsonStr);
		}

		public JsonData this[string key]{
			get { return data[key]; }
		}

		public T Get<T>(string key){
			return  (T)Convert.ChangeType(data[key].ToString(), typeof(T));
		}

		public T Get<T>(string key, T defaultVal){
			var dict = (IDictionary)data;
			if (dict.Contains (key))
				return  (T)Convert.ChangeType (data [key].ToString (), typeof(T));
			else
				return defaultVal;
		}
	}
}

